package com.jykj.modbus.utils;

import java.math.BigInteger;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * 数学相关工具包
 */
public class MathUtils {

    /**
     * 四舍五入保留两位小数返回字符串
     */
    public static String rounded(Double f) {
        DecimalFormat df = new DecimalFormat("#.00");
        return df.format(f);
    }

    /**
     * @param: [content]
     * @return: int
     * @description: 十六进制(2字节)转十进制
     */
    public static Double hex16To10(String strHex){
        if (strHex.length() != 4) {
            throw new NumberFormatException("Wrong length: " + strHex.length() + ", must be 4.");
        }
        int ret = Integer.parseInt(strHex, 16);
        ret = ((ret & 0x8000) > 0) ? (ret - 0x10000) : (ret);
        return (double) ret;
    }

    /**
     * @param: [content]
     * @return: int
     * @description: 十六进制(4字节)转十进制
     */
    public static Double hex16EightTo10(String strHex){
        if (strHex.length() != 8) {
            throw new NumberFormatException("Wrong length: " + strHex.length() + ", must be 8.");
        }
        int ret = Integer.parseInt(strHex, 16);
        ret = ((ret & 0x8000) > 0) ? (ret - 0x10000) : (ret);
        return (double) ret;
    }

    /**
     * 10进制转16进制
     * @param n 10进制数
     * @return 16进制字符串
     */
    public static String intToHex(int n) {
        StringBuffer s = new StringBuffer();
        String a;
        char []b = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
        while(n != 0){
            s = s.append(b[n%16]);
            n = n/16;
        }
        a = s.reverse().toString();
        return a;
    }

    /** 计算校验位 ，返回十六进制校验位 */
    public static String makeCheckSum(String data) {
        int dSum = 0;
        int length = data.length();
        int index = 0;
        // 遍历十六进制，并计算总和
        while (index < length) {
            String s = data.substring(index, index + 2); // 截取2位字符
            dSum += Integer.parseInt(s, 16); // 十六进制转成十进制 , 并计算十进制的总和
            index = index + 2;
        }

        int mod = dSum % 256; // 用256取余，十六进制最大是FF，FF的十进制是255
        String checkSumHex = Integer.toHexString(mod); // 余数转成十六进制
        length = checkSumHex.length();
        if (length < 2) {
            checkSumHex = "0" + checkSumHex;  // 校验位不足两位的，在前面补0
        }
        return checkSumHex;
    }

    /**
     * 16进制字符串进行减法计算,当结果为一位时,加0补位。
     * @param reductionStr
     * @return
     */
    public static String hex16Math(String reductionStr , String minuendStr){
        String endStr = "";
        Integer digits = 0;
        String s = reductionStr.replaceAll("\\+", "");
        System.out.println("s:"+s);
        long l1 = Long.parseLong(reductionStr.replaceAll("\\+", ""), 16);
        long l2 = Long.parseLong(minuendStr.replaceAll("\\+", ""), 16);
        System.out.println("l1:"+l1);
        System.out.println("l2:"+l2);
        Long i1 = Long.parseLong(reductionStr.replaceAll("\\+", ""), 16) -
                Long.parseLong(minuendStr.replaceAll("\\+", ""), 16);
        System.out.println("l1-l2="+i1);
        if (i1.toString().length() == 1){
            endStr = "0"+i1.toString();
        }
        return endStr;
    }

    public static String hex16Math1(String reductionStr , String minuendStr){
        String endStr = "";
        Integer digits = 0;
        long l1 = Long.parseLong(reductionStr);
        long l2 = Long.parseLong(minuendStr);
        System.out.println("l1:"+l1);
        System.out.println("l2:"+l2);
        Long i1 = Long.parseLong(reductionStr) - Long.parseLong(minuendStr);
        System.out.println("l1-l2="+i1);
        if (i1.toString().length() == 1){
            endStr = "0"+i1.toString();
        }
        return endStr;
    }

    /**
     * 十六进制转十进制
     * @param content 16进制字符串
     * @return 10进制数
     */
    public static int covert(String content){
        int number=0;
        String [] HighLetter = {"A","B","C","D","E","F"};
        Map<String,Integer> map = new HashMap<>();
        for(int i = 0;i <= 9;i++){
            map.put(i+"",i);
        }
        for(int j= 10;j<HighLetter.length+10;j++){
            map.put(HighLetter[j-10],j);
        }
        String[]str = new String[content.length()];
        for(int i = 0; i < str.length; i++){
            str[i] = content.substring(i,i+1);
        }
        for(int i = 0; i < str.length; i++){
            number += map.get(str[i])*Math.pow(16,str.length-1-i);
        }
        return number;
    }

}
